package org.vsoc.modules.license.controller;

import java.io.*;
import java.net.URLEncoder;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;
import org.vsoc.modules.license.entity.License;
import org.vsoc.modules.license.service.ILicenseService;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

 /**
 * @Description: license操作
 * @Author: jeecg-boot
 * @Date:   2025-06-26
 * @Version: V1.0
 */
@Slf4j
@Tag(name="license操作")
@RestController
@RequestMapping("/license/license")
public class LicenseController extends JeecgController<License, ILicenseService> {

	 @Autowired
	private ILicenseService licenseService;
	
	/**
	 * 分页列表查询
	 *
	 * @param license
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "license操作-分页列表查询")
	@Operation(summary = "license操作-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(License license,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<License> queryWrapper = QueryGenerator.initQueryWrapper(license, req.getParameterMap());
		Page<License> page = new Page<License>(pageNo, pageSize);
		IPage<License> pageList = licenseService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	 /**
	  * 许可证生成
	  * @param license
	  * @return
	  */
	 @AutoLog(value = "license操作-许可证生成")
	 @Operation(summary = "license操作-许可证生成")
	 @GetMapping(value = "/generateLicense")
	 public Result<?> generateLicense(License  license) {
		try {
			Boolean result = licenseService.generateLicense(license);
			//编辑
			if( result ==  true){
				licenseService.updateById(license);
			}
		}catch (Exception e){
			log.error("generate license falied:"+ e+"license:"+license);
			return Result.error("许可证生成失败！");
		}
		 return Result.OK("许可证生成成功！");
	 }
	//下载许可证
	@AutoLog(value = "license操作-许可证下载")
	@Operation(summary = "license操作-许可证下载")
	@GetMapping("/download")
	public void  downloadFile( @RequestParam(name = "fileName", required = false) String fileName,
									 License license,
									 HttpServletRequest request,
									 HttpServletResponse response) {

		 try {
			 licenseService.downloadFile(fileName,license,request,response);
		 }catch (Exception e){
			 log.error("文件下载失败", e);
		 }


	}
	
	/**
	 * 添加
	 *
	 * @param license
	 * @return
	 */
	@AutoLog(value = "license操作-添加")
	@Operation(summary = "license操作-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody License license) {
		licenseService.save(license);
		return Result.OK("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param license
	 * @return
	 */
	@AutoLog(value = "license操作-编辑")
	@Operation(summary = "license操作-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<?> edit(@RequestBody License license) {
		licenseService.updateById(license);
		return Result.OK("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "license操作-通过id删除")
	@Operation(summary = "license操作-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		licenseService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "license操作-批量删除")
	@Operation(summary = "license操作-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.licenseService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "license操作-通过id查询")
	@Operation(summary = "license操作-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		License license = licenseService.getById(id);
		return Result.OK(license);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param license
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, License license) {
      return super.exportXls(request, license, License.class, "license操作");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, License.class);
  }

}
